﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using MAT.Model;

namespace MAT.Controllers
{
    class ctrManageCategory
    {
        private frmManageCategory _frmManageCategory = null;

        public frmManageCategory FrmManageCategory
        {
            get { return _frmManageCategory; }
            set { _frmManageCategory = value; }
        }

        MAT_Context context = MATContext.getContext();
        List<CATEGORY> ca = null;
        List<ASSET> assets = null;

        private string getID()
        {
            ca = context.CATEGORies.ToList();
            string a = ca[ca.Count - 1].CATEGORY_ID.ToString();
            int dem = Convert.ToInt16(a.Substring(a.Length - 3));
            if (dem >= 99)
                a = "" + ++dem;
            else
                if (dem >= 9)
                    a = "0" + ++dem;
                else
                    a = "00" + ++dem;
            return "C" + a;
        }

        private bool check()
        {
            if (_frmManageCategory.txtCategoryName.Text == "")
            {
                MessageBox.Show("Name is empty");
                _frmManageCategory.txtCategoryName.Focus();
                return false;
            }
            else
                return true;
        }

        public void frmManageCategory_Load()
        {
            _frmManageCategory.cmbCategory.DataSource = context.CATEGORies.ToList();
            _frmManageCategory.cmbCategory.DisplayMember = "CATEGORY_NAME";
            _frmManageCategory.cmbCategory.ValueMember = "CATEGORY_ID";

            _frmManageCategory.txtCategoryName.GotFocus += new EventHandler(txtCategoryName_GotFocus);
            _frmManageCategory.txtCategoryName.MouseClick += new MouseEventHandler(txtCategoryName_GotFocus);
        }

        public void btnReset_Click()
        {
            _frmManageCategory.txtCategoryName.Text = "";
            _frmManageCategory.txtQuantity.Text = "";
        }

        public void btnAdd_Click()
        {
            if (_frmManageCategory.btnExecute.Text == "Add")
            {
                if (check())
                {
                    if (context.CATEGORies.FirstOrDefault(ca => ca.CATEGORY_NAME == _frmManageCategory.txtCategoryName.Text) == null)
                    {
                        CATEGORY category = new CATEGORY();
                        category.CATEGORY_ID = _frmManageCategory.txtCategoryID.Text;
                        category.CATEGORY_NAME = _frmManageCategory.txtCategoryName.Text;
                        category.QUANTITY = 0;
                        try
                        {
                            context.CATEGORies.AddObject(category);
                            context.SaveChanges();
                            MessageBox.Show("SUCCESSFUL!");
                            frmManageCategory_Load();
                        }
                        catch { }
                        _frmManageCategory.btnDelete.Enabled = true;
                        _frmManageCategory.btnEdit.Enabled = true;
                        _frmManageCategory.cmbCategory.Enabled = true;
                        _frmManageCategory.txtCategoryID.Text = "";
                        _frmManageCategory.txtCategoryName.Text = "";
                        _frmManageCategory.txtQuantity.Text = "";
                        _frmManageCategory.btnReset.Visible = false;
                        _frmManageCategory.btnExecute.Text = "New";
                        _frmManageCategory.btnClose.Text = "Close";
                    }
                    else
                        MessageBox.Show("CATEGORY is exist!");
                }
                return;
            }
            if (_frmManageCategory.btnExecute.Text == "New")
            {
                _frmManageCategory.btnExecute.Text = "Add";
                _frmManageCategory.btnDelete.Enabled = false;
                _frmManageCategory.btnEdit.Enabled = false;
                _frmManageCategory.cmbCategory.Enabled = false;
                _frmManageCategory.txtCategoryID.Text = "";
                _frmManageCategory.txtCategoryName.Text = "";
                _frmManageCategory.txtQuantity.Text = "";
                _frmManageCategory.txtCategoryID.Text = getID();
                _frmManageCategory.btnClose.Text = "Cancel";
                _frmManageCategory.btnReset.Visible = true;
                return;
            }
        }

        public void btnDelete_Click()
        {
            CATEGORY category = context.CATEGORies.FirstOrDefault(ca => ca.CATEGORY_ID == _frmManageCategory.txtCategoryID.Text);
            if (category != null)
            {
                //List all assets have category_ID is deleted
                assets = context.ASSETs.Where(ass => ass.CATEGORY_ID == category.CATEGORY_ID).ToList();
                foreach (ASSET a in assets)
                {
                    context.ASSETs.DeleteObject(a);
                    context.SaveChanges();
                }
                try
                {
                    context.CATEGORies.DeleteObject(category);
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    frmManageCategory_Load();
                }
                catch { }
            }
            else
            {
                MessageBox.Show("Not Found!");
            }
            return;
        }

        public void btnClose_Click()
        {
            if (_frmManageCategory.btnClose.Text == "Close")
            {
                _frmManageCategory.Close();
                MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
            }
            if (_frmManageCategory.btnClose.Text == "Cancel")
            {
                _frmManageCategory.btnClose.Text = "Close";
                _frmManageCategory.btnDelete.Enabled = true;
                _frmManageCategory.btnEdit.Enabled = true;
                _frmManageCategory.cmbCategory.Enabled = true;
                _frmManageCategory.txtCategoryID.Text = "";
                _frmManageCategory.txtCategoryName.Text = "";
                _frmManageCategory.txtQuantity.Text = "";
                _frmManageCategory.btnReset.Visible = false;
                _frmManageCategory.btnExecute.Text = "New";
                return;
            }
        }

        public void btnEdit_Click()
        {
            CATEGORY category = context.CATEGORies.FirstOrDefault(ca => ca.CATEGORY_ID == _frmManageCategory.txtCategoryID.Text);
            if (category != null)
            {
                if (context.CATEGORies.FirstOrDefault(ca => ca.CATEGORY_NAME.ToLower() == _frmManageCategory.txtCategoryName.Text.ToLower()) == null)
                {
                    try
                    {
                        category.CATEGORY_NAME = _frmManageCategory.txtCategoryName.Text;
                        context.SaveChanges();
                        MessageBox.Show("SUCCESSFUL!");
                        frmManageCategory_Load();
                    }
                    catch { }
                }
                else
                    MessageBox.Show("CATEGORY NAME is exist!");
            }
            else
            {
                MessageBox.Show("Not Found");
            }
            return;
        }

        public void cmbCategoryName_SelectedIndexChanged()
        {
            try
            {
                string a = _frmManageCategory.cmbCategory.SelectedValue.ToString();
                CATEGORY categories = context.CATEGORies.FirstOrDefault(ca => ca.CATEGORY_ID == a);
                if (categories != null)
                {
                    _frmManageCategory.txtCategoryID.Text = categories.CATEGORY_ID;
                    _frmManageCategory.txtCategoryName.Text = categories.CATEGORY_NAME;
                    _frmManageCategory.txtQuantity.Text = categories.QUANTITY.ToString();
                }
            }
            catch { }
        }

        public void txtCategoryName_KeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnAdd_Click();
        }

        void txtCategoryName_GotFocus(object sender, EventArgs e)
        {
            _frmManageCategory.txtCategoryName.SelectAll();
        }
    }
}
